package flc.ast.adapter;

import android.graphics.Color;
import android.widget.SeekBar;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.FileUtils;
import com.chad.library.adapter.base.provider.BaseItemProvider;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;

import flc.ast.R;
import flc.ast.bean.MyZipBean;
import stark.common.basic.adaptermutil.StkProviderMultiAdapter;
import stark.common.basic.adaptermutil.StkSingleSpanProvider;

public class ZipAdapter extends StkProviderMultiAdapter<MyZipBean> {

    public ZipAdapter() {
        addItemProvider(new StkSingleSpanProvider<>(80));
        addItemProvider(new ZipProvider());
    }

    private boolean isZip = false;

    public void setZip(boolean zip) {
        isZip = zip;
    }

    private class ZipProvider extends BaseItemProvider<MyZipBean> {
        @Override
        public int getItemViewType() {
            return StkProviderMultiAdapter.TYPE_VIEW;
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_zip;
        }

        @Override
        public void convert(@NonNull BaseViewHolder baseViewHolder, MyZipBean bean) {
            baseViewHolder.setImageResource(R.id.ivZipItemImg, getImgIcon(bean.getPath()));
            baseViewHolder.setText(R.id.tvZipItemName, bean.getName());
            baseViewHolder.setText(R.id.tvZipItemSize, bean.getSize());
            SeekBar sbItem = baseViewHolder.getView(R.id.sbZipItem);
            sbItem.setProgress(bean.getProgress());
            baseViewHolder.setText(R.id.tvZipItemNum, bean.getProgress() + "%");
            if (bean.getProgress() == 100) {
                baseViewHolder.setImageResource(R.id.ivZipItemImg, isZip ? R.drawable.ayasuobao : R.drawable.awenjianbao);
                baseViewHolder.setText(R.id.tvZipItemNum, isZip ? getContext().getString(R.string.zip_comp) : getContext().getString(R.string.unzip_comp));
                baseViewHolder.setTextColor(R.id.tvZipItemNum, Color.parseColor("#554FFF"));
            }
        }
    }

    //根据后缀获取图标
    private int getImgIcon(String path) {
        String suffix = FileUtils.getFileExtension(path);
        int icon;
        if (suffix.equalsIgnoreCase("pdf")) {
            icon = R.drawable.apdf;
        } else if (suffix.equalsIgnoreCase("ppt") || suffix.equalsIgnoreCase("pptx")) {
            icon = R.drawable.appt;
        } else if (suffix.equalsIgnoreCase("txt")) {
            icon = R.drawable.atxt;
        } else if (suffix.equalsIgnoreCase("doc") || suffix.equalsIgnoreCase("docx")) {
            icon = R.drawable.adoc;
        } else if (suffix.equalsIgnoreCase("xls") || suffix.equalsIgnoreCase("xlsx")) {
            icon = R.drawable.axls;
        } else if (suffix.equalsIgnoreCase("rar") || suffix.equalsIgnoreCase("zip")) {
            icon = R.drawable.ayasuobao;
        } else {
            icon = R.drawable.awenjianbao;
        }
        return icon;
    }
}
